#include "OgreWidget.h"

OgreWidget::OgreWidget(QWidget *parent)
:QWidget(parent,Qt::MSWindowsOwnDC),mRoot(NULL),mSceMgr(NULL),mRenderWindow(NULL),mCamera(NULL),mViewPort(NULL)
{
	
}

void OgreWidget::initOgre()
{
	using namespace Ogre;

	String plungConfigName="Plugins.cfg";
	String resConfigName="resources.cfg";
	String renderWinName="QtOgreRenderWindow";
	String renderSysName="Direct3D9 Rendering Subsystem";

	Root *ogreRoot=new Root(plungConfigName);
	mRoot=ogreRoot;
	LogManager::getSingleton().setLogDetail(Ogre::LL_NORMAL);
	Ogre::ConfigFile cf;
	cf.load(resConfigName);

	Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();

	Ogre::String secName, typeName, archName;
	while (seci.hasMoreElements())
	{
		secName = seci.peekNextKey();
		Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
		Ogre::ConfigFile::SettingsMultiMap::iterator i;
		for (i = settings->begin(); i != settings->end(); ++i)
		{
			typeName = i->first;
			archName = i->second;
			Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
		}
	}

	Ogre::RenderSystemList::const_iterator pRend = ogreRoot->getAvailableRenderers().begin();
	while (pRend != ogreRoot->getAvailableRenderers().end())
	{
		Ogre::String rName = (*pRend)->getName();
		if (rName == renderSysName)
		{
			break;
		}			
		pRend++;
	}
	Ogre::RenderSystem *rsys = *pRend;
	ogreRoot->setRenderSystem(rsys);

	rsys->setConfigOption("VSync", "No");
	rsys->setConfigOption( "Full Screen", "No" );
	ogreRoot->initialise(false);

	//create window
	show();
	setAttribute(Qt::WA_PaintOnScreen);
	setAttribute(Qt::WA_NoSystemBackground);
	Ogre::NameValuePairList params;
	params["externalWindowHandle"]=Ogre::StringConverter::toString((size_t)winId());
	mRenderWindow=ogreRoot->createRenderWindow(renderWinName,width(),height(),false,&params);

}

void OgreWidget::initCreate()
{
	using namespace Ogre;

	Ogre::Root* root=Ogre::Root::getSingletonPtr();

	//create sceneManager
	SceneManager* sceMgr = root->createSceneManager(Ogre::ST_GENERIC,"generalSceneManager");
	mSceMgr=sceMgr;

	//create Camera
	Ogre::Camera *cam = sceMgr->createCamera("camera");
	mCamera=cam;
	cam->setNearClipDistance(5);
	cam->setFarClipDistance(0);
	cam->setPosition(130, 30, 240);
	cam->lookAt(100,0,200);

	//create Viewport
	Ogre::Viewport *vp = mRenderWindow->addViewport(cam);
	mViewPort=vp;
	ColourValue background(0.1,0.1,0.1);
	vp->setBackgroundColour(background);
	cam->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));

	//create frameListener
	root->addFrameListener(this);
}

QPaintEngine* OgreWidget::paintEngine() const
{
	return NULL; 
}

void OgreWidget::paintEvent(QPaintEvent *)
{
	if(mRoot)
		mRoot->renderOneFrame();
}

void OgreWidget::resizeEvent(QResizeEvent *e)
{
	mRenderWindow->resize(width(), height());
	mRenderWindow->windowMovedOrResized();
	mCamera->setAspectRatio((Ogre::Real)mViewPort->getActualWidth() / (Ogre::Real)mViewPort->getActualHeight()); 
}